<?php
class BaseReportForm extends BaseFormFilterPropel
{


    public function getTemplate(){
        return "dispaly";
    }

    public function getModelName(){
        return "Employee";
    }
    public function getFields(){
        $fields = array();
        foreach ($this as $k =>$field){
            $fields[] = $k;
        }


        //print_r($fields);exit;

        return $fields;
    }


    public function getGroupByFields($key = null){
        $arr  = array(

        'all' => 'All'
        , EmployeePeer::SECTION_ID    => 'Section'
        , EmployeePeer::DEPARTMENT_ID => 'Department'
        , EmployeePeer::ID => 'Employee'
        );
        return $key ? $arr[$key] : $arr;
    }


    public  function getDisplayFields()
    {
        if( isset( $this->display_fields) ){
            return $this->display_fields;
        }
        $fields = $this->getFields();

        $index = array_search('order_by',$fields);
        if(isset($index)){
            unset($fields[$index]);
        }

        $index = array_search('group_by',$fields);
        if(isset($index)){
            unset($fields[$index]);
        }
        return $fields;
    }


    public static $order_by_type = array(
    'Ascending' =>'Ascending', 'Descending' =>'Descending'
    );
    public function getOrderByFields(){
        return array(

        EmployeePeer::CODE  => 'employee code'
        , EmployeePeer::CARD_ID  => 'card id '
        , EmployeeI18nPeer::FIRST_NAME  => 'name'
        , EmployeePeer::HIRING_DATE  => 'hiring date'
        );
    }

    public function setup()
    {
        $this->setWidgets(
        array('employee_id' =>new sfWidgetFormEmployee() ,
        'department_id' => new sfWidgetFormPropelChoice(array('model'=>'CodeDepartment','add_empty'=>globals::CHOOSE_EMPTY_VALUE)) ,
        'section_id' => new sfWidgetPropelAjaxBind(array('model'=>'CodeSection','change_widget'=>'department_id','add_empty'=>globals::CHOOSE_EMPTY_VALUE)) ,
        'leave_type_id'=>new sfWidgetFormPropelChoice(array('model'=>'CodeLeaveType')) ,
        'leave_start'=> new sfWidgetFormTime(),
        'leave_end'=> new sfWidgetFormTime(),
        'order_by'=> new sfWidgetFormOrderByChoice(
        array(
        'choices'  => array('Fields'=>$this->getOrderByFields(),'Type'=>self::$order_by_type))),
        ));

        $this->setValidators(
        array(
        'employee_id' =>new sfValidatorEmployee(array('required'=>false)) ,
        'department_id' => new sfValidatorPropelChoice(array('model'=>'CodeDepartment','required'=>false)) ,
        'section_id' => new sfValidatorPropelChoice(array('model'=>'CodeSection','required'=>false)) ,
        'leave_type_id'=>new sfValidatorPropelChoice(array('model'=>'CodeLeaveType','required'=>false)) ,
        'leave_start'=> new sfValidatorTime(array('required'=>false)),
        'leave_end'=> new sfValidatorTime(array('required'=>false)),
        'order_by'=> new sfValidatorOrderByChoice(array('choices'=>array_merge(array_keys($this->getOrderByFields()),array_keys(self::$order_by_type)),'required'=>false)),
        ));


        $this->widgetSchema->setNameFormat('report[%s]');

    }

    protected function addOrderByColumnCriteria(Criteria $criteria, $field, $values){
        //$colname = $this->getColname($field);
        //die('asd');
        if(is_array($values) && count($values) > 0){
            $type = null;
            $field = null;
            foreach ($values as $k =>$v ){
                if($v=='Ascending' || $v=='Descending'){
                    $type = $v;
                }else{
                    $field = $v;

                }

            }//each

            $type = $type ? $type : 'Ascending';

            if($field){

                switch ($field) {
                    case EmployeeI18nPeer::FIRST_NAME :
                        //$criteria->addJoin(EmployeePeer::ID , EmployeeI18nPeer::ID, Criteria::RIGHT_JOIN  );
                        $criteria->add(EmployeeI18nPeer::CULTURE ,sfContext::getInstance()->getUser()->getCulture() );
                        break;

                    default:
                        break;
                }
                call_user_func_array( array($criteria,sprintf('add%sOrderByColumn',ucfirst($type))) , array($field)  );

            }


        }

    }

    protected function addGroupByColumnCriteria(Criteria $criteria, $field, $value=null){


        switch ($value) {
            case EmployeePeer::SECTION_ID:

                if($this->getModelName()!='Employee'){
                    $criteria->addJoin(constant($this->getModelName().'Peer::EMPLOYEE_ID'),EmployeePeer::ID);
                }
                $criteria->addJoin($value,CodeSectionPeer::ID);
                $criteria->addAscendingOrderByColumn(CodeSectionPeer::ID);
                sfConfig::set('group_by' , $this->getGroupByFields($value) );

                break;
            case EmployeePeer::DEPARTMENT_ID :
                if($this->getModelName()!='Employee'){
                    $criteria->addJoin(constant($this->getModelName().'Peer::EMPLOYEE_ID'),EmployeePeer::ID);
                }
                $criteria->addJoin( $value , CodeDepartmentPeer::ID );
                $criteria->addAscendingOrderByColumn(CodeDepartmentPeer::ID);
                sfConfig::set('group_by' , $this->getGroupByFields($value) );
                break;
            default:
                break;
        }


    }





}
